package com.lagou.paydemo.controller;

import com.lagou.paydemo.utils.UuidUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lixingwei
 * @date 2021-01-24 17:39:21
 */
@RestController
public class PayController {
    private static final List<String> failList = new ArrayList<>();

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @GetMapping("/order")
    public void order() {
        String id = UuidUtil.uuid();
        rabbitTemplate.convertAndSend("ex.pay", "rk.pay", id);
    }

    @GetMapping("/pay")
    public String pay() {
        Object o = rabbitTemplate.receiveAndConvert("queue.pay");
        if (o == null) {
            return "fail";
        } else {
            return "success";
        }
    }

    @GetMapping("/fail")
    public List<String> fail() {
        String failId = (String) rabbitTemplate.receiveAndConvert("queue.pay.failed");
        if (failId != null && !failList.contains(failId)) {
            failList.add(failId);
        }
        return failList;
    }
}
